package com.vf.admin.miaodong.controller;

import com.vf.admin.common.BaseController;
import com.vf.admin.common.Constants;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.dao.AfterSensitiveDao;
import com.vf.admin.miaodong.entity.AppSensitive;
import com.vf.admin.miaodong.service.AfterSensitiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 敏感词管理
 */
@Controller
@RequestMapping("/after/sensitive")
public class AfterSensitiveController extends BaseController {

    @Autowired
    AfterSensitiveService appSensitiveService;

    /**
     * 获取所有敏感词
     */
    @RequestMapping("/list")
    public String getAllUser(SplitPageBean splitPage, String sensi, String startdate, String enddate) {
        Map<String, Object> map = new HashMap<>();
        map.put("sensi", sensi);
        map.put("startdate", startdate);
        map.put("enddate", enddate);

        Map<Integer,String> statesMap = Constants.getTongStateMap();

        List<AppSensitive> maps = appSensitiveService.findList(map, splitPage);
        request.setAttribute("sensi",sensi);
        request.setAttribute("startdate",startdate);
        request.setAttribute("enddate",enddate);
        request.setAttribute("statesMap",statesMap);
        request.setAttribute("list",maps);
        request.setAttribute("splitPage",splitPage);
        return this.sucess("/web/after/sensitive/list");
    }


    /**
     * 添加修改敏感词
     */
    @RequestMapping("/save")
    @ResponseBody
    public Result save(AppSensitive appSensitive) {
        try {
            if (StringUtils.isEmpty(appSensitive.getSensi())) {
                return errorJSON("敏感内容不能为空");
            }if (StringUtils.isEmpty(appSensitive.getState())) {
                return errorJSON("敏感状态不能为空");
            }

            if(appSensitive.getId() != null){
                AppSensitive appSensitive1 = appSensitiveService.get(appSensitive.getId());
                appSensitive1.setSensi(appSensitive.getSensi());
                appSensitive1.setState(appSensitive.getState());
                appSensitiveService.save(appSensitive1);
            }else{
                appSensitive.setCreatetime(new Date());
                appSensitiveService.save(appSensitive);
            }
            //更新敏感词
            Map<String,Object> map=new HashMap<>(16);
            map.put("state",1);
            List<AppSensitive> minganci=appSensitiveService.findList(map,null);
            if(minganci.size()>0) {
                //清空敏感词
                Constants.keyWordSet.clear();
                //重新录入
                for (AppSensitive sensitive : minganci) {
                    Constants.keyWordSet.add(sensitive.getSensi());
                }
            }
            return sucessJSON("修改成功");
        } catch (Exception e) {
            return errorJSON("网络出现异常，请稍后再试");
        }
    }


    /**
     * 获取敏感词详情
     */
    @RequestMapping(value = "/edit")
    public String edit(Integer id) {
        try {
            Map<Integer,String> statesMap = Constants.getTongStateMap();
            AppSensitive byId = appSensitiveService.get(id);
            request.setAttribute("byId", byId);
            request.setAttribute("statesMap",statesMap);
            return sucess("/web/after/sensitive/edit");
        } catch (Exception e) {
            return this.error("系统异常");
        }
    }


}
